<?php
class Admin_ArticleController extends Apps_Controller_Action_Admin {

	public function readAction() {
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter
		            ->select()
		            ->from('article', null)
		            ->columns(array(
		                    'id' => 'article.id',
		                    'title' => 'article.title',
		                    'type' => new Zend_Db_Expr('CASE article.type
		                                                WHEN '. Model_DbTable_Article::TYPE_ABOUT .' THEN "About"
		                                                WHEN '. Model_DbTable_Article::TYPE_SERVICE .' THEN "Service"
		                                                WHEN '. Model_DbTable_Article::TYPE_CAREER .' THEN "Career"
		                                                WHEN '. Model_DbTable_Article::TYPE_SYSTEM_SHOP .' THEN "System shop"
		                                                WHEN '. Model_DbTable_Article::TYPE_NOMAL .' THEN "Normal"
		                                                END'),
		                    'status' => 'article.status',
		                    'status_label' => new Zend_Db_Expr('CASE article.status WHEN 1 THEN "Yes" WHEN 0 THEN "No" END'),
		                    ));

		$grid = new Grid_Article_Article('grid-article');
		$grid->setCaption('List Article');
		$grid->setSource(new OeZend_Widget_Grid_Source_ZendDbSelect($select));
		$grid->setView(new OeZend_Widget_Grid_View());
		$grid->setDisablePaginator();

		$grid->deploy();

		$this->view->grid = $grid;
	}

	//ADD
	public function createAction() {
		$rq 		= $this->_request;
		$f 			= new Admin_Form_AddArticle();
		$tbArticle	= new Model_DbTable_Article();

		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$data = $f->getValues();

				$this->_flashMessenger->addMessage(
					$tbArticle->safelyInsert($data)
					? self::$msgAddSuccess
					: self::$msgAddFailed
				);

				$this->_helper->redirector('read', null, null);
			}
		}
		$this->view->form = $f;
	}

	//EDIT Article
	public function updateAction() {
		$rq = $this->_request;
		$id = $rq->getParam('id');

		$tbArticle = new Model_DbTable_Article();

		if (!$id) {
			$this->_helper->redirector('read');
		}

		$f = new Admin_Form_AddArticle();
		if ($rq->isPost()) {
			if ($f->isValid($rq->getParams())) {
				$tbArticle 	= new Model_DbTable_Article();

				// Upload thumbnail - replace thumbnail if changed
				$rs = $tbArticle->getArticleById($id)->query()->fetch();
				$data = $f->getValues();
				$rT = $tbArticle->safelyUpdate($data, array('id = ?' => $id));
				if (!$rT) {
					$this->_flashMessenger->addMessage(self::$msgUpdateFailed);
				} else {
					$this->_flashMessenger->addMessage(self::$msgUpdateSuccess);
				}
				$this->_helper->redirector('read');
			}
		}
		$f->setLegend('Edit Article');
		$f->populate($tbArticle->fetchRow(array('id = ?' => $id))->toArray());
		$this->view->form = $f;
		$this->render('create');
	}

	//DELETE
	public function deleteAction() {
		$rq = $this->_request;
		$id = $rq->getParam('id');

		if (!$id) {
			$this->_helper->redirector('read');
		}

		$tbArticle = new Model_DbTable_Article();
		$tbArticle = new Model_DbTable_Article();

		if (is_numeric($id)) {
			$this->_flashMessenger->addMessage($tbArticle->deleteArticleById($id)
				? self::$msgDeleteSuccess
				: self::$msgDeleteError);
		} elseif (is_array($id)) {
			foreach ($id as $v) {
				$tbArticle->deleteArticleById($v);
			}
			$this->_flashMessenger->addMessage(self::$msgDeleteSuccess);
		}

		$this->_helper->redirector('read');
	}

	public function changeValueAction() {
		$this->disableLayout(true, true);
		$rq = $this->_request;
		$param  = $rq->getParam('p');
		$id 	= intval($rq->getParam('id'));
		$tbArticle = new Model_DbTable_Article();
		if(!($r = $tbArticle->fetchRow(array('id = ?' => $id))->toArray())) {
			echo '<script>window.location=window.location</script>
				<a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
				. $this->getBacktrackUrl()
				. '">Update Error (Record not found), please wait ...</a>';
		}

		switch ($param) {
			case 'public':
				($r['status'] == Model_DbTable_Article::STATUS_ACTIVE) ?
					$tbArticle->safelyUpdate(array('status'  => Model_DbTable_Article::STATUS_INACTIVE),
										  	 array('id = ?'	  => $id)) :
					$tbArticle->safelyUpdate(array('status'  => Model_DbTable_Article::STATUS_ACTIVE),
										   	 array('id = ?'  => $id));
				break;
			default: break;
		}

		echo '<script>document.location.reload()</script>
			<a style="padding:10px 0;display:block;width:100%;text-align:center" href="'
			. $this->getBacktrackUrl()
			. '">Update successfully, please wait ...</a>';
	}

}